jQuery.extend({
    'zoombox': (function () {
        var m = $("<div class=\'modal fade\' id=\'myModal\' tabindex=\'-1\' role=\'dialog\' aria-labelledby=\'myModalLabel\' aria-hidden=\'true\'></div>");

        var cnt = "";
        return {
            'show': function (type, obj) {

                switch (type) {

                    case 'ajax': mytitle = "出现错误"; showclass = "alert alert-danger";
                        break;

                    case 'ajaxOK': mytitle = "操作成功"; showclass = "alert alert-success";
                        break;

                    default: mytitle = "出现错误"; showclass = "alert alert-danger";
                }
                cnt = "<div class=\'modal-dialog\'>\n";
                cnt = cnt + "<div class=\'modal-content\'>\n";
                cnt = cnt + "<div class=\'modal-header\' >\n";
                cnt = cnt + "<button type=\'button\' class=\'close\' data-dismiss=\'modal\' aria-hidden=\'true\'>&times;</button>\n";
                cnt = cnt + "<h4 class=\'modal-title\' id=\'myModalLabel\'>" + mytitle + "</h4>\n";
                cnt = cnt + "</div>\n";
                cnt = cnt + "<div class=\'modal-body\'>\n";
                cnt = cnt + "<p class=\"" + showclass + "\">" + obj.message + "</p>\n";
                cnt = cnt + "</div>\n";
                cnt = cnt + "<div class=\'modal-footer\'>\n";
                cnt = cnt + "<button type=\'button\' class=\'btn btn-default\' data-dismiss=\'modal\'>Close</button>\n";
                cnt = cnt + "</div>\n";
                cnt = cnt + "</div>\n";
                cnt = cnt + "</div>\n";
                m.html(cnt);
                m.modal('show');
            },
            'hide': function () {
                m.modal('hide');
            }
        };
    })(),
    'copyRight': 'redrangon',
    'removeUploadedImage': function (_this) {
        $(_this).parents('.thumbuper').remove();
    }
});
function submitAjax(parms) {	
    if (!parms.query) parms.query = "";
    parms.query += "&userhash=" + Math.random();

    $.ajax({
        "url": parms.url,
        "type": "post",
        "data": parms.query,
        "success": function (data) {  
        
            var tmp = null;
            try {
                tmp = $.parseJSON(data);
            }
            catch (e)
            {}
     finally { 
    	 if(tmp) {
                    data = tmp;
                    $.zoombox.hide();
                    if (parseInt(data.statusCode) == 200) {

                        $.zoombox.show('ajaxOK', data);
                        setTimeout(function () {
                            if (data.callbackType == 'forward') {
                                if (data.forwardUrl && data.forwardUrl != '') {
                                    if (data.forwardUrl == 'reload')
                                        window.location.reload();
                                    else if (data.forwardUrl == 'back') {
                                    	window.history.back();
                                    }
                                    else
                                        window.location.href = data.forwardUrl;
                                }
                            }
                            else {
                                if (data.forwardUrl && data.forwardUrl != '') {
                                    $.zoombox.hide();
                                    submitAjax({ 'url': data.forwardUrl, 'target': parms.target });
                                }
                                else window.location.reload();
                            }
                        }, 500);
                    } else if (parseInt(data.statusCode) == 300) {
                        $.zoombox.show('ajax', data);
                    } else {
                        $.zoombox.show('ajax', data);
                    }
                }
                else {

                }
                return data.statusCode;
            }
        }
    });
}


function formsubmit() {
	
    var _this = this;
    var status = false;
    var query;
    var target = $(_this).attr('direct');
    if (!target || target == '') target = 'datacontent'; 
    try
    {
    	for ( instance in CKEDITOR.instances )
    		CKEDITOR.instances[instance].updateElement();
    		query = $(":input",_this).serialize()+'&userhash='+Math.random();
    		$(":input",_this).not('.ckeditor').each(function(){
    			var _this = this;
    			var data = xvars($(this));
    			if(!data.result && !status){
    				$(_this).parents(".control-group").addClass("error");
    				$.zoombox.show('ajax',data);
    				status = true;
    			}
    		});
    }
    catch(e){
    	
    }
    query = $(":input", _this).serialize() + '&userhash=' + Math.random();
   
    if (status) return false;
   
    if (!$(_this).attr('action') || $(_this).attr('action') == '') return false;
   // alert("ok1");
    try
    {
    //在这里运行代码
 
    $("form").validate({ debug: true })
//alert("ok2");
    if ($("form").valid()) {
    	
    submitAjax({ "url": $(_this).attr('action'), "query": query, "target": target }); }

    }
    catch(err)
      {
    	 submitAjax({ "url": $(_this).attr('action'), "query": query, "target": target }); 
      }
   
    return false;
}

function AjaxLink(url,query)
{
    submitAjax({ "url":url, "query": query, "target": "_self"});
}
$(function () {
    $("form").on('submit', formsubmit);
});
